layui.use(['layer','jquery','form','upload','element'],function () {
    let form = layui.form;
    let layer = layui.layer;
    let $ = layui.$;
    let upload = layui.upload;
    let element = layui.element;
    //返回功能
    $('#goBack').click(function () {
        parent.tabParentChange(3);
    })
    //刷新功能
    $('#refresh').click(function () {
        location.reload();
    })
    //课程分类按钮点击
    $('#selectCourseClass').click(function () {
        layer.open({
            type: 2,
            title : "选择分类",
            area : [ '550px', '450px' ],
            shade : 0,
            offset: 'auto',
            content:'select-course-class.html',
        })
    });
    $(document).on("mouseenter mouseleave", ".file-iteme", function (event) {
        if (event.type === "mouseenter") {
            //鼠标悬浮
            $(this).children(".handle").fadeIn("fast");
        } else if (event.type === "mouseleave") {
            //鼠标离开
            $(this).children(".handle").hide();
        }
    });

    var imgFiles;
    var imgIndex;
    var imgUploadCount = 0;
    //上传图片
    upload.render({
        elem: "#selectCourseImg",
        url: "/course/uploadCourseImg",
        field: 'image',
        accept: 'images',//上传文件类型
        size: 2048,
        choose: function (obj) {
            imgUploadCount++;
            obj.preview(function(index, file, result){
                imgIndex = index;
                $('#courseImg').attr('src',result);
            });
            if(imgUploadCount != 1){
                delete imgFiles[imgIndex];
            }
            imgFiles = obj.pushFile();
            element.progress('imgProgress', '99%');
        },
        done:function (res, index, upload) {
            if (res.data.code == 200) {
                $('#url').val(res.data.src);
                layer.msg('上传图片成功');
                element.progress('imgProgress', '100%');
            }
        }
    });
    var videoFiles;
    var videoIndex;
    var videoUploadCount = 0;
    //上传视频
    upload.render({
        elem: "#selectCourseVideo",
        url: "/course/uploadCourseVideo",
        field: 'video',
        accept: 'video',//上传文件类型
        size: 409600,
        choose: function (obj) {
            videoUploadCount++;
            obj.preview(function(index, file, result){
                videoIndex = index;
                $('#courseVideo').attr('src',result);
            });
            if(videoUploadCount != 1){
                delete videoFiles[videoIndex];
            }
            videoFiles = obj.pushFile();
            element.progress('videoProgress', '99%');
        },
        done:function (res, index, upload) {
            if (res.data.code == 200) {
                $('#courseUrl').val(res.data.src);
                layer.msg('上传视频成功');
                element.progress('videoProgress', '100%');
            }
        }
    });
    //监听视频时长
    document.getElementById('courseVideo').addEventListener("canplay",function(){
        $('#courseTime').val(Math.floor(this.duration));
    });
    //创建人赋值
    var user = JSON.parse(localStorage.getItem('user'));
    $('#userId').val(user.data.userId);
    $('#createId').val(user.data.userId);
    //图片视频输入框、价格输入框、分类输入框验证
    form.verify({
        url: function (value, item) {
            if(value == ''){
                return '请上传图片';
            }
        },
        courseUrl: function (value, item) {
            if(value == ''){
                return '请上传视频';
            }
        },
        classId: function (value, item) {
            if(value == ''){
                return '请选择课程';
            }
        },
        courseNprice: function (value, item) {
            if(value == ''){
                return '请输入价格';
            }
            var regPos = /^[0-9]+.?[0-9]*$/; //非负浮点数
            if(!regPos.test(value)){
                return '价格必须是数字';
            }
        }
    })
    //添加课程
    form.on('submit(sub)',function (obj) {
        $.ajax({
            url: '/course/addCourse',
            data: obj.field,
            type: 'post',
            dataType: 'json',
            success: function (data) {
                var course = data.data;
                if(course.courseId != null){
                    //添加课程审核记录
                    $.ajax({
                        url: '/courseRecord/addCourseRecord',
                        data: {
                            courseId: course.courseId,
                            launchId: user.data.userId,
                            result: 2,
                            type: 2,
                            remarks: '添加课程 等待审核'
                        },
                        type: 'post',
                        dataType: 'json',
                        success: function (data) {
                            if(data.data.id != null){
                                $.ajax({
                                    url: '/log/addLog',
                                    type: 'post',
                                    dataType: 'json',
                                    data: {
                                        tableName: 'js_course',
                                        type: 2,
                                        userId: user.data.userId,
                                        remark: '添加课程',
                                        logType: 2
                                    },
                                    success: function (data) {
                                        if(data.data.logId != null){
                                            location.reload();
                                            parent.addTab(16);
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
        return false;
    });
})